package cn.ms.doctor.Controller;

import cn.ms.doctor.Entity.Dept;
import cn.ms.doctor.Service.DeptService;
import cn.ms.doctor.Utils.DataStandardUtils;
import cn.ms.doctor.Utils.GlobalMessage;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.Map;

@RestController
@RequestMapping("/Dept")
public class DeptController {

    @Resource
    DeptService deptService;

    @RequestMapping("/isLogin/getDepts")
    public Object getDepts(){
        return new GlobalMessage(200,0,null,deptService.getDepts());
    }

    @RequestMapping("/isLogin/getDeptsAndUserCount")
    public Object getDeptsAndUserCount(@RequestBody Map<String,Object> params){
        if (DataStandardUtils.standardKeyIsEmpty(params,"pageIndex","pageSize")) {
            return new GlobalMessage(200,2,"非法请求...",null);
        }
        Map<String,Object> data=deptService.getDeptsAndUserCount(params);
        return new GlobalMessage(200,0,null,data);
    }

    @RequestMapping("/isLogin/isSystemAdmin/updateDeptNameByDeptId")
    public Object updateDeptNameByDeptId(@RequestBody Dept dept){
        if (StringUtils.isEmpty(dept.getDeptId())||StringUtils.isEmpty(dept.getDeptName())) {
            return  new GlobalMessage(200,2,"非法请求...",null);
        }
        if (!deptService.updateDeptNameByDeptId(dept)) {
            return  new GlobalMessage(200,3,"出现异常,请联系管理员处理...",null);
        }
        return new GlobalMessage(200,0,null,"修改成功...");
    }

    @RequestMapping("/isLogin/isSystemAdmin/addDept")
    public Object addDept(@RequestBody Dept dept){
        if (StringUtils.isEmpty(StringUtils.isEmpty(dept.getDeptName()))) {
            return  new GlobalMessage(200,2,"非法请求...",null);
        }
        if (!deptService.addDept(dept)){
            return new GlobalMessage(200,0,"出现异常,请联系管理员处理...",null);
        }
        return new GlobalMessage(200,0,null,"添加成功...");
    }
    @RequestMapping("/isLogin/isSystemAdmin/deleteDeptByDeptId")
    public Object deleteDeptByDeptId(@RequestBody Dept dept){
        if (StringUtils.isEmpty(StringUtils.isEmpty(dept.getDeptId()))) {
            return  new GlobalMessage(200,2,"非法请求...",null);
        }
        if(!deptService.deleteDeptByDeptId(dept.getDeptId())){
            return new GlobalMessage(200,3,"删除失败,请查看科室是否有职员,或者出现异常...",null);
        }
        return new GlobalMessage(200,0,null,"删除成功...");
    }

}
